<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    body {
      height: 2000px;
      background: linear-gradient(pink, red, blue);
    }

    #top {
      position: fixed;
      bottom: 40px;
      right: 30px;
    }
  </style>
</head>

<body>
  <button id="top">回到顶部</button>
  <script>
    var top = document.getElementById("top")
    top.onclick = function () {
      var mybody = document.documentElement || document.body
      mybody.scrollTop = 0;
    }

  </script>
</body>

</html>